package com.mikgonz.farkle;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.google.web.bindery.requestfactory.shared.Receiver;
import com.mikgonz.farkle.shared.FarkleRequestFactory;
import com.mikgonz.farkle.shared.FarkleRequestFactory.ChatRequest;
import com.mikgonz.farkle.shared.FarkleRequestFactory.FriendshipRequest;
import com.mikgonz.farkle.shared.FarkleRequestFactory.UserRequest;
import com.mikgonz.farkle.shared.FriendshipProxy;
import com.mikgonz.farkle.shared.UserProxy;

public class AsyncRPC extends AsyncTask<String, Void, Object> {

	public static final String REGISTER = "register";
	public static final String LOGIN = "login";
	public static final String CHECK = "check";
	public static final String SEND_MSG = "send message";
	public static final String START_FRIENDSHIP = "start friendship";
	public static final String RESPOND_TO_FR = "respond friendship";
	public static final String NOT_FOUND = "not found";
	public static final String GET_USER = "get user";
	public static final String GET_FR = "get friendship";
	public static final String ACTION = "action";
	public static final String ERROR = "error";
	
	private Context c;

	private String currentAction;

	private UserProxy user;
	private FriendshipProxy friendship;
	private Boolean b;

	public AsyncRPC(Context c) {
		this.c = c;
	}

	@Override
	protected Object doInBackground(String... params) {
		String action = params[0];

		FarkleRequestFactory factory = Util.getRequestFactory(c,
				FarkleRequestFactory.class);

		currentAction = null;

		try {
			if (action.equals(REGISTER)) {
				currentAction = REGISTER;
				return register(factory, params);

			} else if (action.equals(LOGIN)) {
				currentAction = LOGIN;
				return login(factory, params);

			} else if (action.equals(CHECK)) {
				currentAction = CHECK;
				return check(factory, params);

			} else if (action.equals(SEND_MSG)) {
				currentAction = SEND_MSG;
				return sendMsg(factory, params);

			} else if (action.equals(START_FRIENDSHIP)) {
				currentAction = START_FRIENDSHIP;
				return startFriendship(factory, params);

			} else if (action.equals(RESPOND_TO_FR)) {
				currentAction = RESPOND_TO_FR;
				return respond(factory, params);

			} else if (action.equals(GET_USER)) {
				currentAction = GET_USER;
				return getUser(factory, params);

			} else if (action.equals(GET_FR)) {
				currentAction = GET_FR;
				return getFriendRequest(factory, params);

			} else {
				currentAction = NOT_FOUND;
				return null;
			}
		} catch (Exception e) {
			currentAction = ERROR;
			return e.getMessage();
		}

	}

	private Object register(FarkleRequestFactory factory, String[] params) {
		user = null;
		UserRequest request = factory.userRequest();
		request.register(params[1], params[2], params[3]).fire(
				new Receiver<UserProxy>() {
					@Override
					public void onSuccess(UserProxy u) {
						user = u;
					}
				});

		return user;
	}

	private Object login(FarkleRequestFactory factory, String[] params) {
		user = null;
		UserRequest request = factory.userRequest();
		String id = params[3];

		request.logIn(params[1], params[2],
				(id == null) ? null : Long.parseLong(id), params[4]).fire(
				new Receiver<UserProxy>() {

					@Override
					public void onSuccess(UserProxy u) {
						user = u;
					}

				});
		return user;
	}

	private Object check(FarkleRequestFactory factory, String[] params) {
		b = null;
		UserRequest request = factory.userRequest();
		request.checkAvailability(params[1]).fire(new Receiver<String>() {

			@Override
			public void onSuccess(String s) {
				b = new Boolean(s);
			}

		});
		return b;
	}

	private Object sendMsg(FarkleRequestFactory factory, String[] params) {
		ChatRequest request = factory.chatRequest();

		request.sendChatMessage(Long.parseLong(params[1]),
				Long.parseLong(params[2]), params[3]).fire();

		return null;
	}

	private Object startFriendship(FarkleRequestFactory factory, String[] params) {

		friendship = null;

		FriendshipRequest request = factory.friendshipRequest();

		request.startFriendship(params[1], Long.parseLong(params[2]), params[3])
				.fire(new Receiver<FriendshipProxy>() {

					@Override
					public void onSuccess(FriendshipProxy fr) {
						friendship = fr;
					}

				});
		return friendship;
	}

	private Object respond(FarkleRequestFactory factory, String[] params) {
		user = null;

		Log.w(getClass().getName(), "String: " + params[2]);
		Log.w(getClass().getName(), "String as Boolean: "
				+ new Boolean(params[2]));
		Log.w(getClass().getName(), "String as boolean: "
				+ new Boolean(params[2]).booleanValue());

		FriendshipRequest request = factory.friendshipRequest();

		request.acceptOrRejectFriendship(Long.parseLong(params[1]),
				new Boolean(params[2]).booleanValue()).fire(
				new Receiver<UserProxy>() {
					@Override
					public void onSuccess(UserProxy u) {
						user = u;
					}

				});
		return user;
	}

	private Object getUser(FarkleRequestFactory factory, String[] params) {
		user = null;

		UserRequest request = factory.userRequest();

		request.readUser(Long.parseLong(params[1])).fire(
				new Receiver<UserProxy>() {

					@Override
					public void onSuccess(UserProxy u) {
						user = u;

					}

				});

		return user;
	}

	private Object getFriendRequest(FarkleRequestFactory factory,
			String[] params) {
		friendship = null;
		FriendshipRequest request = factory.friendshipRequest();
		request.readFriendship(Long.parseLong(params[1])).fire(
				new Receiver<FriendshipProxy>() {

					@Override
					public void onSuccess(FriendshipProxy fr) {
						friendship = fr;
					}

				});

		return friendship;
	}

	public String getAction() {
		return currentAction;
	}

}
